home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / include / obstack.h < prev    next >
C/C++ Source or Header  |  1996-01-30  |  20KB  |  417 lines

  1. /* This is file obstack.h */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /* obstack.h - object stack macros
  8.    Copyright (C) 1988 Free Software Foundation, Inc.
  9.  
  10. This program is free software; you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License as published by the
  12. Free Software Foundation; either version 1, or (at your option) any
  13. later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24.  
  25. In other words, you are welcome to use, share and improve this program.
  26. You are forbidden to forbid anyone else to use, share and improve
  27. what you give them.   Help stamp out software-hoarding!  */
  28.  
  29.  
  30. /* Summary:
  31.  
  32. All the apparent functions defined here are macros. The idea
  33. is that you would use these pre-tested macros to solve a
  34. very specific set of problems, and they would run fast.
  35. Caution: no side-effects in arguments please!! They may be
  36. evaluated MANY times!!
  37.  
  38. These macros operate a stack of objects.  Each object starts life
  39. small, and may grow to maturity.  (Consider building a word syllable
  40. by syllable.)  An object can move while it is growing.  Once it has
  41. been "finished" it never changes address again.  So the "top of the
  42. stack" is typically an immature growing object, while the rest of the
  43. stack is of mature, fixed size and fixed address objects.
  44.  
  45. These routines grab large chunks of memory, using a function you
  46. supply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
  47. by calling `obstack_chunk_free'.  You must define them and declare
  48. them before using any obstack macros.
  49.  
  50. Each independent stack is represented by a `struct obstack'.
  51. Each of the obstack macros expects a pointer to such a structure
  52. as the first argument.
  53.  
  54. One motivation for this package is the problem of growing char strings
  55. in symbol tables.  Unless you are "fascist pig with a read-only mind"
  56. [Gosper's immortal quote from HAKMEM item 154, out of context] you
  57. would not like to put any arbitrary upper limit on the length of your
  58. symbols.
  59.  
  60. In practice this often means you will build many short symbols and a
  61. few long symbols.  At the time you are reading a symbol you don't know
  62. how long it is.  One traditional method is to read a symbol into a
  63. buffer, realloc()ating the buffer every time you try to read a symbol
  64. that is longer than the buffer.  This is beaut, but you still will
  65. want to copy the symbol from the buffer to a more permanent
  66. symbol-table entry say about half the time.
  67.  
  68. With obstacks, you can work differently.  Use one obstack for all symbol
  69. names.  As you read a symbol, grow the name in the obstack gradually.
  70. When the name is complete, finalize it.  Then, if the symbol exists already,
  71. free the newly read name.
  72.  
  73. The way we do this is to take a large chunk, allocating memory from
  74. low addresses.  When you want to build a symbol in the chunk you just
  75. add chars above the current "high water mark" in the chunk.  When you
  76. have finished adding chars, because you got to the end of the symbol,
  77. you know how long the chars are, and you can create a new object.
  78. Mostly the chars will not burst over the highest address of the chunk,
  79. because you would typically expect a chunk to be (say) 100 times as
  80. long as an average object.
  81.  
  82. In case that isn't clear, when we have enough chars to make up
  83. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  84. so we just point to it where it lies.  No moving of chars is
  85. needed and this is the second win: potentially long strings need
  86. never be explicitly shuffled. Once an object is formed, it does not
  87. change its address during its lifetime.
  88.  
  89. When the chars burst over a chunk boundary, we allocate a larger
  90. chunk, and then copy the partly formed object from the end of the old
  91. chunk to the beginning of the new larger chunk.  We then carry on
  92. accreting characters to the end of the object as we normally would.
  93.  
  94. A special macro is provided to add a single char at a time to a
  95. growing object.  This allows the use of register variables, which
  96. break the ordinary 'growth' macro.
  97.  
  98. Summary:
  99.         We allocate large chunks.
  100.         We carve out one object at a time from the current chunk.
  101.         Once carved, an object never moves.
  102.         We are free to append data of any size to the currently
  103.           growing object.
  104.         Exactly one object is growing in an obstack at any one time.
  105.         You can run one obstack per control block.
  106.         You may have as many control blocks as you dare.
  107.         Because of the way we do it, you can `unwind' a obstack
  108.           back to a previous state. (You may remove objects much
  109.           as you would with a stack.)
  110. */
  111.  
  112.  
  113. /* Don't do the contents of this file more than once.  */
  114.  
  115. #ifndef __OBSTACKS__
  116. #define __OBSTACKS__
  117.  
  118. /* We use subtraction of (char *)0 instead of casting to int
  119.    because on word-addressable machines a simple cast to int
  120.    may ignore the byte-within-word field of the pointer.  */
  121.  
  122. #ifndef __PTR_TO_INT
  123. #define __PTR_TO_INT(P) ((P) - (char *)0)
  124. #endif
  125.  
  126. #ifndef __INT_TO_PTR
  127. #define __INT_TO_PTR(P) ((P) + (char *)0)
  128. #endif
  129.  
  130. struct _obstack_chunk           /* Lives at front of each chunk. */
  131. {
  132.   char  *limit;                 /* 1 past end of this chunk */
  133.   struct _obstack_chunk *prev;  /* address of prior chunk or NULL */
  134.   char  contents[4];            /* objects begin here */
  135. };
  136.  
  137. struct obstack          /* control current object in current chunk */
  138. {
  139.   long  chunk_size;             /* preferred size to allocate chunks in */
  140.   struct _obstack_chunk* chunk; /* address of current struct obstack_chunk */
  141.   char  *object_base;           /* address of object we are building */
  142.   char  *next_free;             /* where to add next char to current object */
  143.   char  *chunk_limit;           /* address of char after current chunk */
  144.   int   temp;                   /* Temporary for some macros.  */
  145.   int   alignment_mask;         /* Mask of alignment for each object. */
  146.   struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk.  */
  147.   void (*freefun) ();           /* User's function to free a chunk.  */
  148. };
  149.  
  150. #ifdef __STDC__
  151.  
  152. /* Do the function-declarations after the structs
  153.    but before defining the macros.  */
  154.  
  155. void obstack_init (struct obstack *obstack);
  156.  
  157. void * obstack_alloc (struct obstack *obstack, int size);
  158.  
  159. void * obstack_copy (struct obstack *obstack, void *address, int size);
  160. void * obstack_copy0 (struct obstack *obstack, void *address, int size);
  161.  
  162. void obstack_free (struct obstack *obstack, void *block);
  163.  
  164. void obstack_blank (struct obstack *obstack, int size);
  165.  
  166. void obstack_grow (struct obstack *obstack, void *data, int size);
  167. void obstack_grow0 (struct obstack *obstack, void *data, int size);
  168.  
  169. void obstack_1grow (struct obstack *obstack, int data_char);
  170. void obstack_ptr_grow (struct obstack *obstack, void *data);
  171. void obstack_int_grow (struct obstack *obstack, int data);
  172.  
  173. void * obstack_finish (struct obstack *obstack);
  174.  
  175. int obstack_object_size (struct obstack *obstack);
  176.  
  177. int obstack_room (struct obstack *obstack);
  178. void obstack_1grow_fast (struct obstack *obstack, int data_char);
  179. void obstack_ptr_grow_fast (struct obstack *obstack, void *data);
  180. void obstack_int_grow_fast (struct obstack *obstack, int data);
  181. void obstack_blank_fast (struct obstack *obstack, int size);
  182.  
  183. void * obstack_base (struct obstack *obstack);
  184. void * obstack_next_free (struct obstack *obstack);
  185. int obstack_alignment_mask (struct obstack *obstack);
  186. int obstack_chunk_size (struct obstack *obstack);
  187.  
  188. #endif /* __STDC__ */
  189.  
  190. /* Non-ANSI C cannot really support alternative functions for these macros,
  191.    so we do not declare them.  */
  192.  
  193. /* Pointer to beginning of object being allocated or to be allocated next.
  194.    Note that this might not be the final address of the object
  195.    because a new chunk might be needed to hold the final size.  */
  196.  
  197. #define obstack_base(h) ((h)->object_base)
  198.  
  199. /* Size for allocating ordinary chunks.  */
  200.  
  201. #define obstack_chunk_size(h) ((h)->chunk_size)
  202.  
  203. /* Pointer to next byte not yet allocated in current chunk.  */
  204.  
  205. #define obstack_next_free(h)    ((h)->next_free)
  206.  
  207. /* Mask specifying low bits that should be clear in address of an object.  */
  208.  
  209. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  210.  
  211. #define obstack_init(h) \
  212.   _obstack_begin ((h), 0, 0, obstack_chunk_alloc, obstack_chunk_free)
  213.  
  214. #define obstack_begin(h, size) \
  215.   _obstack_begin ((h), (size), 0, obstack_chunk_alloc, obstack_chunk_free)
  216.  
  217. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
  218.  
  219. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  220.  
  221. #if defined (__GNUC__) && defined (__STDC__)
  222.  
  223. /* For GNU C, if not -traditional,
  224.    we can define these macros to compute all args only once
  225.    without using a global variable.
  226.    Also, we can avoid using the `temp' slot, to make faster code.  */
  227.  
  228. #define obstack_object_size(OBSTACK)                                    \
  229.   ({ struct obstack *__o = (OBSTACK);                                   \
  230.      (unsigned) (__o->next_free - __o->object_base); })
  231.  
  232. #define obstack_room(OBSTACK)                                           \
  233.   ({ struct obstack *__o = (OBSTACK);                                   \
  234.      (unsigned) (__o->chunk_limit - __o->next_free); })
  235.  
  236. #define obstack_grow(OBSTACK,where,length)                              \
  237. ({ struct obstack *__o = (OBSTACK);                                     \
  238.    int __len = (length);                                                \
  239.    ((__o->next_free + __len > __o->chunk_limit)                         \
  240.     ? _obstack_newchunk (__o, __len) : 0);                              \
  241.    bcopy (where, __o->next_free, __len);                                \
  242.    __o->next_free += __len;                                             \
  243.    (void) 0; })
  244.  
  245. #define obstack_grow0(OBSTACK,where,length)                             \
  246. ({ struct obstack *__o = (OBSTACK);                                     \
  247.    int __len = (length);                                                \
  248.    ((__o->next_free + __len + 1 > __o->chunk_limit)                     \
  249.     ? _obstack_newchunk (__o, __len + 1) : 0),                          \
  250.    bcopy (where, __o->next_free, __len),                                \
  251.    __o->next_free += __len,                                             \
  252.    *(__o->next_free)++ = 0;                                             \
  253.    (void) 0; })
  254.  
  255. #define obstack_1grow(OBSTACK,datum)                                    \
  256. ({ struct obstack *__o = (OBSTACK);                                     \
  257.    ((__o->next_free + 1 > __o->chunk_limit)                             \
  258.     ? _obstack_newchunk (__o, 1) : 0),                                  \
  259.    *(__o->next_free)++ = (datum);                                       \
  260.    (void) 0; })
  261.  
  262. /* These assume that the obstack alignment is good enough for pointers or ints,
  263.    and that the data added so far to the current object
  264.    shares that much alignment.  */
  265.    
  266. #define obstack_ptr_grow(OBSTACK,datum)                                 \
  267. ({ struct obstack *__o = (OBSTACK);                                     \
  268.    ((__o->next_free + sizeof (void *) > __o->chunk_limit)               \
  269.     ? _obstack_newchunk (__o, sizeof (void *)) : 0),                    \
  270.    *((void **)__o->next_free)++ = ((void *)datum);                      \
  271.    (void) 0; })
  272.  
  273. #define obstack_int_grow(OBSTACK,datum)                                 \
  274. ({ struct obstack *__o = (OBSTACK);                                     \
  275.    ((__o->next_free + sizeof (int) > __o->chunk_limit)                  \
  276.     ? _obstack_newchunk (__o, sizeof (int)) : 0),                       \
  277.    *((int *)__o->next_free)++ = ((int)datum);                           \
  278.    (void) 0; })
  279.  
  280. #define obstack_ptr_grow_fast(h,aptr) (*((void **)(h)->next_free)++ = (void *)aptr)
  281. #define obstack_int_grow_fast(h,aint) (*((int *)(h)->next_free)++ = (int)aint)
  282.  
  283. #define obstack_blank(OBSTACK,length)                                   \
  284. ({ struct obstack *__o = (OBSTACK);                                     \
  285.    int __len = (length);                                                \
  286.    ((__o->next_free + __len > __o->chunk_limit)                         \
  287.     ? _obstack_newchunk (__o, __len) : 0);                              \
  288.    __o->next_free += __len;                                             \
  289.    (void) 0; })
  290.  
  291. #define obstack_alloc(OBSTACK,length)                                   \
  292. ({ struct obstack *__h = (OBSTACK);                                     \
  293.    obstack_blank (__h, (length));                                       \
  294.    obstack_finish (__h); })
  295.  
  296. #define obstack_copy(OBSTACK,where,length)                              \
  297. ({ struct obstack *__h = (OBSTACK);                                     \
  298.    obstack_grow (__h, (where), (length));                               \
  299.    obstack_finish (__h); })
  300.  
  301. #define obstack_copy0(OBSTACK,where,length)                             \
  302. ({ struct obstack *__h = (OBSTACK);                                     \
  303.    obstack_grow0 (__h, (where), (length));                              \
  304.    obstack_finish (__h); })
  305.  
  306. #define obstack_finish(OBSTACK)                                         \
  307. ({ struct obstack *__o = (OBSTACK);                                     \
  308.    void *value = (void *) __o->object_base;                             \
  309.    __o->next_free                                                       \
  310.      = __INT_TO_PTR ((__PTR_TO_INT (__o->next_free)+__o->alignment_mask)\
  311.                      & ~ (__o->alignment_mask));                        \
  312.    ((__o->next_free - (char *)__o->chunk                                \
  313.      > __o->chunk_limit - (char *)__o->chunk)                           \
  314.     ? (__o->next_free = __o->chunk_limit) : 0);                         \
  315.    __o->object_base = __o->next_free;                                   \
  316.    value; })
  317.  
  318. #define obstack_free(OBSTACK, OBJ)                                      \
  319. ({ struct obstack *__o = (OBSTACK);                                     \
  320.    void *__obj = (OBJ);                                                 \
  321.    if (__obj >= (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
  322.      __o->next_free = __o->object_base = __obj;                         \
  323.    else (obstack_free) (__o, __obj); })
  324.  
  325. #else /* not __GNUC__ or not __STDC__ */
  326.  
  327. /* The non-GNU macros copy the obstack-pointer into this global variable
  328.    to avoid multiple evaluation.  */
  329.  
  330. extern struct obstack *_obstack;
  331.  
  332. #define obstack_object_size(h) \
  333.  (unsigned) (_obstack = (h), (h)->next_free - (h)->object_base)
  334.  
  335. #define obstack_room(h)         \
  336.  (unsigned) (_obstack = (h), (h)->chunk_limit - (h)->next_free)
  337.  
  338. #define obstack_grow(h,where,length)                                    \
  339. ( (h)->temp = (length),                                                 \
  340.   (((h)->next_free + (h)->temp > (h)->chunk_limit)                      \
  341.    ? _obstack_newchunk ((h), (h)->temp) : 0),                           \
  342.   bcopy (where, (h)->next_free, (h)->temp),                             \
  343.   (h)->next_free += (h)->temp)
  344.  
  345. #define obstack_grow0(h,where,length)                                   \
  346. ( (h)->temp = (length),                                                 \
  347.   (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit)                  \
  348.    ? _obstack_newchunk ((h), (h)->temp + 1) : 0),                               \
  349.   bcopy (where, (h)->next_free, (h)->temp),                             \
  350.   (h)->next_free += (h)->temp,                                          \
  351.   *((h)->next_free)++ = 0)
  352.  
  353. #define obstack_1grow(h,datum)                                          \
  354. ( (((h)->next_free + 1 > (h)->chunk_limit)                              \
  355.    ? _obstack_newchunk ((h), 1) : 0),                                   \
  356.   *((h)->next_free)++ = (datum))
  357.  
  358. #define obstack_ptr_grow(h,datum)                                       \
  359. ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit)                \
  360.    ? _obstack_newchunk ((h), sizeof (char *)) : 0),                     \
  361.   *((char **)(h)->next_free)++ = ((char *)datum))
  362.  
  363. #define obstack_int_grow(h,datum)                                       \
  364. ( (((h)->next_free + sizeof (int) > (h)->chunk_limit)                   \
  365.    ? _obstack_newchunk ((h), sizeof (int)) : 0),                        \
  366.   *((int *)(h)->next_free)++ = ((int)datum))
  367.  
  368. #define obstack_ptr_grow_fast(h,aptr) (*((char **)(h)->next_free)++ = (char *)aptr)
  369. #define obstack_int_grow_fast(h,aint) (*((int *)(h)->next_free)++ = (int)aint)
  370.  
  371. #define obstack_blank(h,length)                                         \
  372. ( (h)->temp = (length),                                                 \
  373.   (((h)->next_free + (h)->temp > (h)->chunk_limit)                      \
  374.    ? _obstack_newchunk ((h), (h)->temp) : 0),                           \
  375.   (h)->next_free += (h)->temp)
  376.  
  377. #define obstack_alloc(h,length)                                         \
  378.  (obstack_blank ((h), (length)), obstack_finish ((h)))
  379.  
  380. #define obstack_copy(h,where,length)                                    \
  381.  (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
  382.  
  383. #define obstack_copy0(h,where,length)                                   \
  384.  (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
  385.  
  386. #define obstack_finish(h)                                               \
  387. ( (h)->temp = __PTR_TO_INT ((h)->object_base),                          \
  388.   (h)->next_free                                                        \
  389.     = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \
  390.                     & ~ ((h)->alignment_mask)),                         \
  391.   (((h)->next_free - (char *)(h)->chunk                                 \
  392.     > (h)->chunk_limit - (char *)(h)->chunk)                            \
  393.    ? ((h)->next_free = (h)->chunk_limit) : 0),                          \
  394.   (h)->object_base = (h)->next_free,                                    \
  395.   __INT_TO_PTR ((h)->temp))
  396.  
  397. #ifdef __STDC__
  398. #define obstack_free(h,obj)                                             \
  399. ( (h)->temp = (char *)(obj) - (char *) (h)->chunk,                      \
  400.   (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
  401.    ? (int) ((h)->next_free = (h)->object_base                           \
  402.             = (h)->temp + (char *) (h)->chunk)                          \
  403.    : ((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0)))
  404. #else
  405. #define obstack_free(h,obj)                                             \
  406. ( (h)->temp = (char *)(obj) - (char *) (h)->chunk,                      \
  407.   (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
  408.    ? (int) ((h)->next_free = (h)->object_base                           \
  409.             = (h)->temp + (char *) (h)->chunk)                          \
  410.    : (int) _obstack_free ((h), (h)->temp + (char *) (h)->chunk)))
  411. #endif
  412.  
  413. #endif /* not __GNUC__ or not __STDC__ */
  414.  
  415. #endif /* not __OBSTACKS__ */
  416.  
  417.